package com.it.controller;

import com.it.pojo.Dept;
import com.it.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dept")
public class DeptController {

    @Autowired
    private DeptService deptService;

    @PostMapping("/add")
    public boolean addDept(@RequestBody Dept dept) {
        return deptService.insertDept(dept);
    }

    @GetMapping("/get/{id}")
    public Dept get(@PathVariable("id") Long id) {
        return deptService.selectById(id);
    }

    @GetMapping("/getAll")
    public List<Dept> getAll() {
        return deptService.selectAll();
    }

    @Autowired
    private DiscoveryClient client;

    @GetMapping("/discover")
    public Object discover() {
        // 获取微服务列表的清单
        List<String> services = client.getServices();
        System.out.println("discovery -> services : " + services);

        // 根据ServiceID获取具体的微服务信息
        List<ServiceInstance> instances = client.getInstances("SPRINGCLOUD-PROVIDER-DEPT");
        for (ServiceInstance instance : instances) {
            System.out.println(instance.getInstanceId());   // 服务的描述信息
            System.out.println(instance.getHost());         // 服务的host
            System.out.println(instance.getPort());         // 服务端口号
            System.out.println(instance.getUri());          // 服务地址(host+port)
            System.out.println(instance.getServiceId());    // 服务的ServiceID

        }
        return this.client;
    }

}
